<template>
  <div class="weather">
    <Top @getdata="changedata"></Top>
    <div class="out">
      <div class="left">
        <Temp></Temp>
        <Target></Target>
        <HourData></HourData>
      </div>
      <div class="center">
        <BasicData></BasicData>
        <DayData></DayData>
      </div>
      <div class="right">
        <Other></Other>
        <MaxMin></MaxMin>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import Top from './top/index.vue'
import Temp from './Temp/index.vue'
import Target from './target/index.vue'
import HourData from './hourdata/index.vue'
import BasicData from './basicdata/index.vue'
import DayData from './daydata/index.vue'
import Other from './other/index.vue'
import MaxMin from './maxmin/index.vue'
import type{resweather24data,resweatherdata,resqualitydata,DataObject,Data24Object,DataQualityObject} from '@/api/weather/type'
import {
  getWeatherData,
  getWeather24Data,
  getqualitydata
} from '../../api/weather/index'
import { useWeatherStore } from '../../stores/weather'
const weatherStore = useWeatherStore()



// 使用 ref 创建响应式数据
const data = ref<DataObject[]>([])
const datas = ref<Data24Object[]>([])
const datass = ref<DataQualityObject[]>([])

const changedata = (city: string = '北京') => {
  initdata(city)
  init24data(city)
  initqualitydata(city)
}
const initdata = async (city: string = '北京') => {
  let result:resweatherdata = await getWeatherData(city)
  data.value = result.data.daily
  weatherStore.modifydata(data.value)
}
const init24data = async (city: string = '北京') => {
  let result:resweather24data = await getWeather24Data(city)
  datas.value = result.data.hourly
  weatherStore.modify24data(datas.value)
}
const initqualitydata = async (city: string = '北京') => {
  let result:resqualitydata = await getqualitydata(city)
  datass.value = result.data.daily
  weatherStore.modifyqualitydata(datass.value)
}
// 在组件挂载后发起请求
onMounted(() => {
  initdata()
  init24data()
  initqualitydata()
})
</script>

<style scoped lang="less">
.weather {
  width: 100%;
  height: 100vh;
  background-color: #efffe9;
  .out {
    display: flex;
    .left {
      width: 25%;
    }
    .center {
      width: 35%;
      margin-left: 100px;
    }
    .right {
      width: 25%;
      margin-left: 80px;
    }
  }
}
</style>
